import { Component, OnInit, Injector } from '@angular/core';
import { ListBase } from 'app/modules/base/list-base';
import { WebMessageService, WebMessage, WebMessageStatus } from '../../services/web-message.service';
import { MySelection } from 'app/modules/base/selection';
import { map } from 'rxjs/operators';

@Component({
	selector: 'm-web-message-list',
	templateUrl: './web-message-list.component.html',
	styleUrls: ['./web-message-list.component.scss'],
})
export class WebMessageListComponent extends ListBase implements OnInit {
	root = '/message/web';
	JSON_stringify=JSON.stringify
	navs = [
		{ title: '全部消息', url: this.root + '/all' },
		{ title: '未读消息', url: this.root + '/unread' },
		{ title: '已读消息', url: this.root + '/read' },
		{ title: '我发送的', url: this.root + '/sent' },
	];
	constructor(private injector: Injector, private webMessageService: WebMessageService) {
		super(injector);
	}
	WebMessageStatus = WebMessageStatus;
	result$ = this.getData('list');
	status$ = this.route.paramMap.pipe(map(param => param.get('status')));
	hideColumn$ = this.status$.pipe(
		map(status => (status === WebMessageStatus.Sent ? ['from', 'recv_time'] : ['to', 'send_time'])),
	);
	selection = new MySelection<WebMessage>('id');

	ngOnInit() {}

	select(arr: WebMessage[]) {
		this.selection.select(...arr);
	}

	async markRead(all: boolean) {
		if (all) {
			await this.webMessageService.batch('status', WebMessageStatus.Read, null, true);
			this.reload();
			return;
		}
		const ids = this.selection.selected.map(m => m.id);
		await this.webMessageService.batch('status', WebMessageStatus.Read, ids);
		this.selection.clear();
		this.reload();
	}
}
